import router from './router'

import { getToken } from '@/common/auth'

const whiteList = ['/login'] // 跳转白名单 已经是登录页了就不需要再跳了

router.beforeEach((to, from, next) => {
  const hasToken = getToken()
  if (hasToken) {
    if (to.path === '/login') {
      next({
        path: '/',
      })
    } else {
      // 有token 该干嘛干嘛
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      // const query = this.$route.query;
      // this.redirect = query.redirect || '';
      // if(this.redirect) {
      //   this.$router.push({path: this.redirect});
      // }else {
      //   this.$router.push({name: 'Home'});
      // }
      next(`/login?redirect=${to.path}`)
    }
  }
})
